<?php
namespace Home\Controller;
use Think\Controller;


class UserController extends Controller {
      
     //登陆
     function login(){
     	#获取参数
     	$tel= $_POST["telephone"];
     	$pwd=$_POST["password"];
        //模型化
        $user=M("User");//填表名，前缀省略，首字母大写

           //首先进行查询判断是否存在该手机号
        $data=$user->where("user_phone='$tel'")->select();
        if ($data) {
        	# 手机号存在，用户存在
	        //再判断密码是否正确
	        $contion["user_password"]=$pwd;
            $contion["user_phone"]=$tel;
	        $data1=$user->where($contion)->select();
	       // $data=$user->where("user_iphone='$tel'" and "user_password='$pwd'")->select();
	        if ($data1) {
	        	# 存在该用户，登陆成功，返回所有信息
	        	$this->ajaxReturn(array(
	        		"code"=>200,
	        		"message"=>"登陆成功",
	        		"result"=>$data1[0]
	        		),"JSON"); 
	        }else{
	        	$this->ajaxReturn(array(
	        		"code"=>500,
	        		"message"=>"用户名或密码不正确！！"
	        		),"JSON");
	        }
        }
        else{
        	$this->ajaxReturn(array(
	        		"code"=>300,
	        		"message"=>"该用户不存在！"
	        		),"JSON");
        }

     }

//注册
     function register(){
      #获取参数
      $tel= $_POST["telephone"];
      $pwd=$_POST["password"];
        //模型化
        $user=M("User");//填表名，前缀省略，首字母大写

        //首先进行查询判断是否存在该手机号
        $data=$user->where("user_phone='$tel'")->select();
        if ($data) {
          # 手机号存在，
          $this->ajaxReturn(array(
            "code"=>300,
            "message"=>"手机号已存在！"
            ),"JSON");
        }
        else{
          #可以注册
            if ($pwd) {
                # code...
                $newdata["user_phone"]=$tel;
                $newdata["user_password"]=$pwd;
                
                $result=$user->add($newdata);//返回为插入数据的主键

                if ($result) {
                  $result1=$user->where("user_id='$result'")->select();
                  if ($result1) {
                    # code...
                     $this->ajaxReturn(array(
                    "code"=>200,
                    "message"=>"注册成功！",
                    "result1"=>$result1[0]
                    ),"JSON");
                  }else{
                    //注册失败
                    $this->ajaxReturn(array(
                    "code"=>500,
                    "message"=>"查找失败！"
                    ),"JSON");
                  }

                }else{
                    //注册失败
                    $this->ajaxReturn(array(
                    "code"=>500,
                    "message"=>"注册失败！"
                    ),"JSON");
                }
            }else{
                $this->ajaxReturn(array(
                    "code"=>300,
                    "message"=>"密码为空！"
                    ),"JSON");
                }        
        }
     }

   

   //修改密码
     function updatepwd(){

      $oldpwd=$_POST["oldpwd"];
      $newpwd=$_POST["newpwd"];
      $userid=$_POST["userid"];

      $user=M("User");
      //查询原始密码是否正确
      $result=$user->where("user_id='$userid' and user_password='$oldpwd'")->select();
      if ($result) {
        //原始密码正确可以更改
        $newdata["user_id"]=$userid;
        $newdata["user_password"]=$newpwd;
          $data=$user->save($newdata);
          if ($data) {
            # 修改成功
            $this->ajaxReturn(array(
              "code"=>200,
              "message"=>"修改成功！"
              ),"JSON");
          }
          else{
            $this->ajaxReturn(array(
              "code"=>500,
              "message"=>"修改失败！"
              ),"JSON");
          }

      }
      else{
        //密码不正确，不能更改
        $this->ajaxReturn(array(
              "code"=>300,
              "message"=>"密码不正确！"
              ),"JSON");
      }
     
     }

     //修改支付密码
     function updatePaypwd(){

      $oldpwd=$_POST["oldpwd"];
      $newpwd=$_POST["newpwd"];
      $userid=$_POST["userid"];

      $user=M("User");
      //查询原始密码是否正确
      if ($oldpwd) {
        echo "string";
              $result=$user->where("user_id='$userid' and user_paypwd='$oldpwd'")->select();
                  if ($result) {
                    //原始密码正确可以更改
                    $newdata["user_id"]=$userid;
                    $newdata["user_paypwd"]=$newpwd;
                      $data=$user->save($newdata);
                      if ($data) {
                        # 修改成功
                        $this->ajaxReturn(array(
                          "code"=>200,
                          "message"=>"修改成功！"
                          ),"JSON");
                      }
                      else{
                        $this->ajaxReturn(array(
                          "code"=>500,
                          "message"=>"修改失败！"
                          ),"JSON");
                      }

                  }
                  else{
                    //密码不正确，不能更改
                    $this->ajaxReturn(array(
                          "code"=>300,
                          "message"=>"密码不正确！"
                          ),"JSON");
                  }
      }else if(!$oldpwd){
          $newdata["user_id"]=$userid;
          $newdata["user_paypwd"]=$newpwd;
            $data=$user->save($newdata);
            if ($data) {
              # 修改成功
              $this->ajaxReturn(array(
                "code"=>200,
                "message"=>"设置成功！"
                ),"JSON");
            }
            else{
              $this->ajaxReturn(array(
                "code"=>500,
                "message"=>"设置失败！"
                ),"JSON");
            }

        }
         
    }
    
     //更改用户头像
    public function updateheaderimg(){
    $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize=3145728 ;// 设置附件上传大小
        $upload->exts =array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath=  './images/header/'; // 设置附件上传根目录
        $upload->savePath= ''; // 设置附件上传（子）目录  
        $upload->autoSub=false;      
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            // $this->success('上传成功！');
             $userid=$_POST["userid"];

              $user=M("User");
              $data["user_id"]=$userid;           
           
             $data["user_headimage"]=$info["image"]["savepath"].$info["image"]["savename"];             
            $result = $user->save($data);             
             if ($result == 1) {
                $this->ajaxReturn(array("code"=>200,"message"=>"修改头像成功"),"JSON");
             }else{
                 $this->ajaxReturn(array("code"=>500,"message"=>"修改头像失败"),"JSON");
             }
         }
  }


     //更改手机号
  public function updatephone(){
     $user=M("User");
       $id=$_POST["userid"];
       $phone=$_POST["userphone"];
       //添加条件查询
       //$data=$user->where("user_id=$id")->select();
       $newdata["user_id"]=$id;
       $newdata["user_phone"]=$phone;
       $result=$user->save($newdata);//当数据相同时，无法修改，即为原数据        
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"message"=>"修改成功","data"=>$result),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"修改失败","data"=>NULL),"JSON");
       }
  } 
  //修改昵称/姓名
  //http://localhost:18181/NongGuoGuo/index.php/home/user/updatenick
  public function updatenick(){
       $user=M("User");
       $id=$_POST["userid"];
       $nick=$_POST["usernick"];
       //添加条件查询
       $condition["user_id"]=$id;
       $data=$user->where($condition)->select();

       $newdata["user_id"]=$id;
       $newdata["user_nick"]=$nick;
       $result=$user->save($newdata);//当数据相同时，无法修改，即为原数据

            
       if ($result) {

        //更新数据
        $dataa=$user->where("user_id=$id")->select(); 
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"message"=>"更新成功","data"=>$dataa[0]),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"更新失败","data"=>NULL),"JSON");
       }


          //$this->ajaxReturn(array("code"=>200,"message"=>"修改成功","data"=>$result[0]),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"修改失败","data"=>$data[0]),"JSON");
       }
  }
  

  
  //查看个人信息
  public function checkpersonalinfo(){
    $user=M("User");
       $id=$_POST["userid"];
       //添加条件查询
       $data=$user->where("user_id=$id")->select(); 
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"message"=>"查找成功","data"=>$data[0]),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"查找失败","data"=>NULL),"JSON");
       }
  }


  //反馈信息(关于我们)
  public function feelback(){
    $advice=M("Advice");
       $userid=$_POST["userid"];
       $content=$_POST["content"];
       //添加条件查询
       $newdata["user_id"]=$userid;
       $newdata["advice_content"]=$content;
     
       $data=$advice->add($newdata); 
       
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"message"=>"反馈成功","data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"反馈失败","data"=>NULL),"JSON");
       }
  }

  //个人详细信息（多表）
  public function lookinfo(){

     $user=M("User");
         $userid=$_POST["userid"];
         $data["user_id"] = $userid;
         //添加条件查询
          $result=$user  
          ->field("t_user.user_nick,t_user.user_phone,t_profession.profession_job,t_address.address_name")  
          ->join("t_profession on t_user.profession_id=t_profession.profession_id")
          ->join("t_address on t_user.address_id=t_address.address_id")
          ->where($data)
          ->select();      
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"data"=>$result[0]),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
  }




     //查询个人信息
     function inquireUserInfo(){ 

        $type=$_POST["type"];
        $userid=$_POST["userid"];
        $user=M("User");
        if (!$type) {
           $result=$user->where("user_id='$userid'")->select();

          if ($result) {
               $this->ajaxReturn(array(
                  "code"=>200,
                  "message"=>"查询成功！",
                  "data"=>$result[0]
                  ),"JSON");
           }
           else{
               $this->ajaxReturn(array(
                  "code"=>500,
                  "message"=>"查询失败！"
                  ),"JSON");
           }
        }elseif ($type==1) {

          $result=$user
          ->field("t_user.user_nick,t_user.user_headimage,t_address.address_name,t_profession.profession_job,t_user.user_certification")
          ->join("t_profession on t_user.profession_id=t_profession.profession_id")
          ->join("t_address on t_user.address_id=t_address.address_id")
          ->where("user_id='$userid'")->select();

          if ($result) {
               $this->ajaxReturn(array(
                  "code"=>200,
                  "message"=>"查询成功！",
                  "data"=>$result[0]
                  ),"JSON");
           }
           else{
               $this->ajaxReturn(array(
                  "code"=>500,
                  "message"=>"查询失败！"
                  ),"JSON");
           }
        } 
       
     }
   
     ///进行实名认证

     function certification(){

        $userid=$_POST["userid"];
        $usercertificate=$_POST["usercertificate"];

        $user=M("User");

        $result=$user->where("user_id='$userid' and user_certification='$usercertification'")->select();

        if ($result) {
    
          $newdata["user_id"]=$userid;
          $newdata["user_certification"]=$usercertificate;
          $data=$user->save($newdata);
            if ($data) {
              # 修改成功
              $this->ajaxReturn(array(
                "code"=>200,
                "message"=>"认证成功！"
                ),"JSON");
            }
            else{
              $this->ajaxReturn(array(
                "code"=>500,
                "message"=>"认证失败！"
                ),"JSON");
            }
        }

      }


      //进行创建店铺

      function shop(){
        
        $userid=$_POST["userid"];
        $initialshop=$_POST["initialshop"];
        $creatshop=$_POST["creatshop"];
        $user=M("User");

        $result=$user->where("user_id='$userid' and user_shop='$initialshop'")->select();

        if ($result) {
    
          $newdata["user_id"]=$userid;
          $newdata["user_shop"]=$creatshop;

          $data=$user->save($newdata);
            if ($data) {
              # 修改成功
              $this->ajaxReturn(array(
                "code"=>200,
                "message"=>"创建成功！"
                ),"JSON");
            }
            else{
              $this->ajaxReturn(array(
                "code"=>500,
                "message"=>"创建失败！"
                ),"JSON");
            }
        }

      }


  ///忘记密码

      function Forgetpassword(){

       $tel=$_POST["tel"];
       $password=$_POST["password"];
       $user=M("User");
       $result=$user
       ->field("t_user.user_id")
       ->where("user_phone='$tel'")->select();
       $re=$result[0]["user_id"];
       
       $result1=$user->where("user_id='$re'")->select();
        if ($result1) {
    
            $newdata["user_id"]=$re;
            $newdata["user_password"]=$password;

            $data=$user->save($newdata);
              if ($data) {
                # 修改成功
                $this->ajaxReturn(array(
                  "code"=>200,
                  "message"=>"修改成功",
                  "data"=>$result1[0]
                  ),"JSON");
              }
              else{
                $this->ajaxReturn(array(
                  "code"=>500,
                  "message"=>"修改失败！"
                  ),"JSON");
              }
        }
      }

      //查看
  public function lookuserinfo(){
      $user=M("User");
      $data["user_id"]=$_POST["userid"];
       $result=$user  
           ->field("t_user.user_id,t_user.user_nick,t_user.user_headimage") 
          ->where($data)
          ->select();        
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"message"=>"查询成功","data"=>$result[0]),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"查询失败","data"=>NULL),"JSON");
       }  
  }


}